import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../../models/case_list_entity.dart';
import '../../../remote/request.dart';
import '../../../services/api_service.dart';
import '../../../services/common_service.dart';

class NoticeSelectCaseLogic extends GetxController {
  final apiService = Get.find<ApiService>();
  final commonService = Get.find<CommonService>();

  var isReloadList = true.obs;
  var caseList = <CaseInfo>[].obs;

  late int noticeId;

  @override
  void onInit() {
    super.onInit();

    noticeId = Get.arguments;

    _getCaseList();
  }

  void selectCase(int index) => request(() async {
        var res = await apiService.noticeAction('deal',
            id: noticeId,
            caseId: caseList[index].id,
            caseName: caseList[index].caseNameStr);
        if (res != null) {
          Get.back(result: true);
          EasyLoading.showInfo('成交案源成功');
        } else {
          EasyLoading.showError('成交案源失败，请稍后重试!');
        }
      }, showLoading: true, message: '正在成交案源...');

  void _getCaseList() {
    apiService.listAllCase(commonService.getRole()).then((caseList) => {
          isReloadList.value = false,
          if (caseList != null) {this.caseList.addAll(caseList.all!)}
        });
  }
}
